<?php
namespace scripts;
use globalunit\logic\AdminLogic;
use QYS\Protocol\Response;
use QYS\Protocol\Request;
use QYS\Db\Mysql;
use QYS\Log\Log;
use QYS\Db\Redis;
use globalunit\utils\MysqlLocker;
use globalunit\utils\DBHelper;
use globalunit\utils\config;
use globalunit\utils\JWTUtil;
use globalunit\utils\RndName;
use globalunit\utils\Utils;
use globalunit\utils\KeysUtil;
use QYS\Core\Config as QYSConfig;
use globalunit\utils\MyLocker;
use globalunit\model\MenuModel;
use QYS\third\Crypto\XXTEA;
use globalunit\utils\GenID;
use Carbon\Carbon;
use QYS\Core\Config as CoreConfig;
use globalunit\utils\QueueHelper;
use utils\SmsUtil;
use utils\OssUtil;
use QYS\QYS;
use QYS\Db\Mongo;

class CMDADMINLOGIN
{
    //获取路由菜单
    public static function getMenu($request, $response, $rediscli,$mysql){
        $roles = 1;
        if(empty($roles)){
            return ["errcode" => 10001,];
        }
        $menu = AdminLogic::getMenu($roles,$mysql);
        return ["errcode" => 0, 'menu' => $menu];
    }

    //获取全部菜单
    public static function getAllMenu($request, $response, $rediscli,$mysql){
        $menu = AdminLogic::getAllMenu();
        return ["errcode" => 0, 'menu' => $menu];
    }

    public static function index($request, $response, $rediscli,$mysql){
        return ["errcode" => 0,];
    }

    /**
     * 获取所有参数配置
     * @param $request
     * @param $response
     * @param $rediscli
     * @return array
     */
    public static function getConfig($request, $response, $rediscli, $mysql){
        Config::loadconfig();
        $res = Config::getallconfig();

        unset($res['errmsg'] );
        unset($res['errmsgen']);

        return ['errcode'=>0,'data'=>$res];
    }
    /**
     * 设置参数配置
     * @param $request
     * @param $response
     * @param $rediscli
     * @return array
     */
    public static function setConfig($request, $response, $rediscli, $mysql){

        $data   = $request->getString('data');


        if (empty($data)) {
            return ['errcode'=>10001,];
        }

        $arr   = json_decode($data,true);
        $conn       = Mongo::getInstance('mongo1');
        $gamename   = $GLOBALS['GAMENAME'];
        $db         = $conn->$gamename;
        $collection = $db->gameconfig;

        foreach($arr as $k => $v){
            foreach ($v as $kk=>$vv) {
                if (empty($kk)) {
                    continue;
                }
                $collection->updateOne(["_id"=>$k,],['$set'=>[$kk=>$vv]],['upsert'=>true]);
                /*foreach ($v as $kk=>$vv) {
                    $collection->updateOne(["_id"=>$k,],['$set'=>[$kk=>$vv]],['upsert'=>true]);
                }*/
            }
        }


        return ['errcode'=>0,];
    }
}


class AdminLoginCtrl
{

    public function invoke($request,$response,$params)
    {
        $response->addHeader("Content-Type", 'application/json');
        $response->addHeader("Access-Control-Allow-Origin", "*");
        $response->addHeader("Access-Control-Allow-Methods", 'POST, GET, OPTIONS, DELETE');
        $response->addHeader('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
        $response->sendHttpHeader();
        $fname = $params['fname'];
        $admin_id = $request->getString("admin_id");
        $rediscli = Redis::getInstance("redis1");
        $mysql = Mysql::getInstance("mysql1");
        $mysqlLock = new MysqlLocker($mysql);
        $result = null;
        if(isset($admin_id)){
            $mainkey = KeysUtil::get_model_key("lock",$admin_id);
            $lockkey = "LOCK:".$mainkey;
            $lock = new MyLocker($rediscli,$lockkey,10);
            $result = null;
            if(!$lock->islocked()){
                $result= array("errcode" => 10000, "data" =>["a"=>'无法锁定']);
            }else{
                $result = CMDADMINLOGIN::$fname($request,$response,$rediscli,$mysql);
            }
        }else{
            $result = CMDADMINLOGIN::$fname($request,$response,$rediscli,$mysql);
        }
        if($result['errcode'] != 0){
            $mysqlLock->autocommit(false);
            $errcode = $result['errcode'];
            $result['errmsg'] = Config::get('errmsg','e'.$errcode);
        }else{
            $mysqlLock->autocommit(true);
        }
        // Log::info('debug',$request,['fname'=>$fname,'result'=>$result]);
        $response->say(json_encode($result));
    }
}
